package top.chmin.project.cloud.sys.user;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import top.chmin.project.cloud.common.Condition;
import top.chmin.project.cloud.common.IntegerModelColumn;
import top.chmin.project.cloud.common.Model;

import java.util.Arrays;

/**
 * chmin
 * 2024/1/11 9:27
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class User extends Model {
    private String userName;
    private String password;

    public static void main(String[] args) {
        User user = new User();
        user.setId(123);
        user.setUserName("chmin");
        user.setPassword("123456");
        user.forSelect().distinct().id().eq().sub().id().eq().id().eq().finish().selectFirst(UserVo.class);
    }

    public UserCondition forInsert() {
        return new UserCondition().forInsert();
    }

    public UserCondition forUpdate() {
        return new UserCondition().forUpdate();
    }

    public UserCondition forDelete() {
        return new UserCondition().forDelete();
    }

    public UserCondition forSelect() {
        return new UserCondition().forSelect();
    }

    public class UserCondition extends Condition {
        UserCondition parent = null;

        private UserCondition() {
        }

        private UserCondition(UserCondition condition) {
            this.parent = condition;
        }

        private UserCondition forInsert() {
            UserCondition condition = new UserCondition();
            condition.setSql("INSERT INTO sys_user(user_name, password, create_by, update_by, create_time, update_time) VLAUES (?, ?, ?, ?, ?, ?)");
            condition.setValueList(Arrays.asList(userName, password, getCreateBy(), getUpdateBy(), getCreateTime(), getUpdateTime()));
            return condition;
        }

        private UserCondition forUpdate() {
            UserCondition condition = new UserCondition();
            if (userName != null) {
                condition.getUpdateList().add("user_name = ?");
                condition.getValueList().add(userName);
            }
            if (password != null) {
                condition.getUpdateList().add("password = ?");
                condition.getValueList().add(password);
            }
            condition.setSql("UPDATE sys_user#update##where##order##limit#");
            return condition;
        }

        private UserCondition forDelete() {
            UserCondition condition = new UserCondition();
            condition.setSql("DELETE FROM sys_user#where##group##order##limit#");
            return condition;
        }

        private UserCondition forSelect() {
            UserCondition condition = new UserCondition();
            condition.modelClass = User.class;
            condition.setSql("SELECT #select# FROM sys_user#where##group##order##limit#");
            return condition;
        }

        public UserCondition distinct() {
            this.distinct = true;
            return this;
        }

        public UserCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public UserCondition sub() {
            return new UserCondition(this);
        }

        public UserCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<UserCondition> {

            private Id() {
                super(UserCondition.this, "id", User.this.getId());
            }
        }
    }
}
